﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FreedomX2.Core.Log;
using System.Reflection;
using FreedomX2.Core.IO;
using System.Collections;

namespace FreedomX2.Core.Dynamic.Load
{
    public class Loader
    {
        public string FilePath { get; set; }
        //public string ClassPath { get; set; }
        public object LastType { get; set; }
        public object LastInstance { get; set; }
        private Loader() { }
        public Loader(string filepath, string classname)
        {
            FilePath = filepath;
            if (FilePath[0] == '\\')
                FilePath = Envior.Ment.PROGRAMROOT + FilePath;


            Assembly ambly = Assembly.LoadFile(FilePath);
            var vInterface = ambly.CreateInstance(classname);
            LastInstance = vInterface;
            if (ambly == null)
            {
                string.Format("Loader.static.GetInterface<T>入参{0}无效！", FilePath).l();
            }

        }
        //public Loader(string filepath, string classpath)
        //{
        //    FilePath = filepath;
        //    if (FilePath[0] == '\\')
        //        FilePath = Envior.Ment.PROGRAMROOT + FilePath;
        //    ClassPath = classpath;
        //}
        public object InvokeDLLMem(string objectPath, object value)
        {
            string classfun = objectPath;
            if (value == null)
            {
                var vType = LastInstance.GetType();
                LastType = vType;
                return vType.GetField(classfun).GetValue(LastInstance);
            }
            else
            {
                var vType = LastInstance.GetType();
                LastType = vType;
                vType.GetField(classfun).SetValue(LastInstance, value);
            }
            return null;
        }
        public object InvokeDLLFun(string objectPath, params object[] param)
        {
            try
            {
                var vType = LastInstance.GetType();
                LastType = vType;
                return vType.GetMethod(objectPath).Invoke(LastInstance, param);
            }
            catch
            {
                return null;
            }
        }
        public List<object> InvokeDLLPubVars(object value)
        {
            return InvokeDLLMem("Vars", value) as List<object>;
        }

        public static object SInvokeDLLMem(string FileAndObject)
        {
            try
            {
                if (FileAndObject.Trim() == string.Empty)
                {
                    "Loader.static.GetInterface<T>入参无效！".l();
                }
                if (FileAndObject.IndexOf(';') == -1)
                {
                    "Loader.static.GetInterface<T>入参无效！".l();
                }
                string filepath = FileAndObject.SplitBy(';', 0, ABSet.Before);
                string classpath = FileAndObject.SplitBy(';', 0, ABSet.After);
                string classfun = classpath.SplitBy('.', -1, ABSet.After);
                classpath = classpath.SplitBy('.', -1, ABSet.Before);
                if (filepath[0] == '\\')
                    filepath = Envior.Ment.PROGRAMROOT + filepath;

                Assembly ambly = Assembly.LoadFile(filepath);
                if (ambly == null)
                {
                    string.Format("Loader.static.GetInterface<T>入参{0}无效！", filepath).l();
                }
                var vInterface = ambly.CreateInstance(classpath);
                return vInterface.GetType().GetField(classfun).GetValue(vInterface);
            }
            catch
            {
                return null;
            }
        }

        public static object SInvokeDLLFun(string FileAndObject, params object[] param)
        {
            try
            {
                if (FileAndObject.Trim() == string.Empty)
                {
                    "Loader.static.GetInterface<T>入参无效！".l();
                }
                if (FileAndObject.IndexOf(';') == -1)
                {
                    "Loader.static.GetInterface<T>入参无效！".l();
                }
                string filepath = FileAndObject.SplitBy(';', 0, ABSet.Before);
                string classpath = FileAndObject.SplitBy(';', 0, ABSet.After);
                string classfun = classpath.SplitBy('.', -1, ABSet.After);
                classpath = classpath.SplitBy('.', -1, ABSet.Before);
                if (filepath[0] == '\\')
                    filepath = Envior.Ment.PROGRAMROOT + filepath;

                Assembly ambly = Assembly.LoadFile(filepath);
                if (ambly == null)
                {
                    string.Format("Loader.static.GetInterface<T>入参{0}无效！", filepath).l();
                }
                var vInterface = ambly.CreateInstance(classpath);
                if (param.Length > 0)
                    return vInterface.GetType().GetMethod(classfun).Invoke(vInterface, param);
                else
                    return vInterface.GetType().GetMethod(classfun).Invoke(vInterface, new object[] { param });
            }
            catch (Exception _ex)
            { string.Format("SInvokeDLLFun is Bug : {0} - {1}", _ex.Message, FileAndObject); }
            return null;
        }
    }
}
